/**
 * Created by AllanXu on 01/03/2016.
 */
define(function (require) {
	var Vue = require('vue'),
		moment = require('moment'),
		Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities');
    var qtid = sessionStorage.qtid;

	var app = new Vue({
		el: '#enroll_basicInfo',
		data: {
            selections: {
                424: []
            },
            marriageList: [],
            educationList: [],
            ageArr: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45],
            moneyArr: [2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 30000, 50000, 100000],
            heightArr: [154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178,180,182,184,186,188,190],
            cityList: [],
            cityLists: [],
			req: {
                'nickname': '',
                'birthday': '',
                'height': '',
				'weight': '',
				'nation': '',
				'nativePlace': '',
				'profession': '',
				'education': '',
                'mateChoice': '',
                'interests': '',
				'marry': '',
                'phone': '',
				'companyName': '',
				'companyType': '',
                'houseStatus': '',
                'carStatus': '',
                'introduction': '',
                'area': '',
                'mateAge': '',
                'mateIncome': '',
                'mateArea': '',
                'mateHeight': '',
                'mateNative': '',
                'mateEducation': '',
                'mateMarry': '',
                'introduction': ''
			}
		},
		methods: {
            loadArea: function (arrName, code, callback) {
                _(Config.SERVICE.COMMON.ADDRLIST, {
                    parentId: code
                }, function (data) {
                    app[arrName] = data.obj[code];
                    app[arrName].unshift({id: '', name: '请选择'});
                    if (typeof callback === 'function')
                        callback();
                });
            },
			init: function () {
                _(Config.DICTIONARY, {
                    parentId: '418,424,437,450,8,6,7,457,463,467,1010,975,340,975'
                }, function (data) {
                    for (var key in data.obj) {
                        data.obj[key].unshift({id: '', name: '请选择'});
                    }

                    app.selections = data.obj;

                    var marriageList = app.removeUnused(data.obj[6]);
                    var educationList = app.removeUnused(data.obj[8]);

                    _(Config.SERVICE.MATES.ENTOLL + '?id=' + qtid, {}, function (data) {
                        for (var key in app.req) {
                            app.req[key] = data.obj[key];
                        }

                        app.restoreCheckList(marriageList, app.req.mateMarry.split(','));
                        app.restoreCheckList(educationList, app.req.mateEducation.split(','));

                        app.marriageList = marriageList;
                        app.educationList = educationList;

                        _(Config.SERVICE.COMMON.ADDRLIST, {
                            parentId: 2
                        }, function (data) {
                            var cityList = data.obj[2];
                            app.restoreCheckList(cityList, app.req.mateArea.split(','));
                            app.cityLists = cityList;
                        });

                        app.loadArea('cityList', 2);
                    });
                });
			},
            restoreCheckList: function (fullListArr, checkedArr) {
                for (var i = 0; i < fullListArr.length; i++) {
                    //console.log(fullListArr[i].name);
                    for (var j = 0; j < checkedArr.length; j++) {
                        if (fullListArr[i].name == checkedArr[j]) {
                            fullListArr[i].checked = true;
                        }
                    }
                }
            },
            getName: function (arr, id) {
                for (var i = 0; i < arr.length; i++)
                    if (arr[i]['id'] == id)
                        return arr[i]['name'];
            },
            removeUnused: function (arr) {
                var _arr = [];
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i].name != '不透露') {
                        _arr.push(arr[i]);
                    }
                }
                return _arr;
            },
            pick: function (listName, i) {
                app.$set(listName + '[' + i + '].checked', !app[listName][i].checked);
            },
            isChecked: function (item) {
                return !!item.checked;
            },
            getCheckedStr: function (arr) {
                var str = '';
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i].checked && arr[i].id!='')
                        str += (arr[i].name + ',');
                }
                return str.substr(0, str.length - 1);
            },
			save: function () {
				var req = {};
				for (var key in app.req) {
                    //console.log(app.req[key]);
                    req['enroll.' + key] = app.req[key];
				}
				req.id = qtid;

                req['enroll.mateMarry'] = app.getCheckedStr(app.marriageList);
                req['enroll.mateEducation'] = app.getCheckedStr(app.educationList);
                req['enroll.mateArea'] = app.getCheckedStr(app.cityLists);

				_(Config.SERVICE.ACTIVITY.ENROLLSAVE, req, function (data) {
					if (data.code == 200 && data.type === 'SUCCESS') {
						alert('保存成功！');
						history.back();
					}
				});
			}
		}
	});

	app.init();
});
